package study.work.自定义排序;

import com.alibaba.fastjson2.JSON;
import study.User;

import java.text.Collator;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author 刘伟涛
 * @Date 2023/11/30 9:17
 **/
public class Demo {
    public static void main(String[] args) {
        List<User> list = Arrays.asList(new User(12, "志四", 25),
                new User(14, "婵五", 24),
                new User(13, "张二", 23));

        Collections.sort(list, new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                //compareTo方法：大于为1，小于为-1，等于为0。
                return o1.getId().compareTo(o2.getId());//大于位置互换，小于位置不变。为正序
            }
        });
        System.out.println("1"+JSON.toJSON(list));
        //lambda表达式
        Collections.sort(list, (o1, o2) -> {

            return o2.getId().compareTo(o1.getId());//大于位置互换，小位置不变。为逆序
        });
        System.out.println("2"+JSON.toJSONString(list));
        //lambda表达式，默认为正序
        list.sort(Comparator.comparing(User::getName));
        System.out.println("3"+JSON.toJSON(list));
        list.sort((o1, o2) -> {
            Collator instance = Collator.getInstance(Locale.CHINA);
            return instance.compare(o1.getName(), o2.getName());
        });
        System.out.println("4"+JSON.toJSON(list));
        List<String> list1 = Arrays.asList("志", "云", "雪", "伟", "薇", "优", "婷", "思", "蕊", "秋", "佩", "欧", "娜", "美", "莉", "科", "嘉", "伊", "惠", "格", "芳", "俄", "迪", "婵", "宝", "阿");
        list1.sort((o1, o2) -> {
            Collator instance = Collator.getInstance(Locale.CHINA);
            return instance.compare(o1, o2);
        });
        System.out.println("5" + JSON.toJSON(list1));
    }
}
